<?php
$dbConn = mysql_pconnect (DBHOST,DBUSER,DBPASS) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db(DBNAME) or mysql_query("CREATE DATABASE `".DBNAME."`;") and mysql_select_db(DBNAME);
check_tables(DBNAME);

function check_tables($db_name) {
	$sql_contents = file_get_contents('db_tables.sql', FILE_USE_INCLUDE_PATH);
	$sql = explode(';',$sql_contents);
	foreach($sql as $s) {
		if(stristr($s,'CREATE TABLE')) {
			$str = explode('`',strstr($s,'`'));
			$table_name = $str[1];
			if(!table_exists($table_name,$db_name)) {
				mysql_query($s) or die('Table: '.$table_name.' '.mysql_error()); //create table
			}
		}
	}
}

function table_exists($table, $db) { 
	$tables = mysql_list_tables ($db); 
	while (list ($temp) = mysql_fetch_array ($tables)) {
		if ($temp == $table) {
			return TRUE;
		}
	}
	return FALSE;
}